import type { Request, ResponseToolkit } from "@hapi/hapi";
import Hapi from "@hapi/hapi";
import { User, roles } from "../../model/index";

let route: Hapi.ServerRoute<Hapi.ReqRefDefaults> = {
  path: "/getUserList",
  method: "get",
  handler: async (r: Request, h: ResponseToolkit) => {
    try {
      const pageSize = 10; //每页显示的数据条数\n' +
      const currentPage = 1; //当前页\n' +
      //   let role = await roles.find()
      let list = await User.find()
        .select("account roles _id avatarUrl")
        .skip(pageSize * (currentPage - 1))
        .limit(pageSize);

      //   list.forEach((item:any)=>{
      //         item.roleName = role.find(res=>res.)
      //   })
      return {
        code: 200,
        data: {
          data: list,
          total: list.length,
        },
        message: "",
      };
    } catch (err: any) {
      return err;
    }
  },
  options: {
    plugins: {
      "hapi-swagger": {
        security: [{ apiKey: [] }],
      },
    },
    tags: ["api"],
  },
};
export default route;
